package com.example.ecommercemarketing.helper;

import com.example.ecommercemarketing.entity.Merchant;
import com.example.ecommercemarketing.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class RecommendProductHelper {
    /**
     * 查询商户的推荐商品列表（模拟了数据库查询和复杂的业务处理）
     *
     * @param merchant 商户对象
     * @return 该商户的推荐商品列表
     */
    public List<Product> queryRecommendProduct(Merchant merchant) {
        log.info("Querying products for merchant:{} ,on thread:{}", merchant.getId(), Thread.currentThread().getName());

        // 模拟I/O延迟
        try {
            Thread.sleep(1 + new Random().nextInt(20));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId("id" + i);
            product.setName("商户" + merchant.getId() + "产品" + i);
            product.setMerId(merchant.getId());
            product.setMerLevel(merchant.getLevel()); // 添加商户等级用于排序
            productList.add(product);
        }

        /*if ("id5".equals(merchant.getId())) {
            //用于测试异常情况
            int kk = 1 / 0;
        }*/

        return productList;
    }
}
